package mathematics.geometry;

import utils.Point3D;

public class BezierCurve extends PolylineGeometry
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -194930810694950707L;

	public BezierCurve(Point3D p0, Point3D p1, Point3D p2, Point3D p3, float weight, int nbSegment)
	{
		super();
		double step = 1.0 / nbSegment;

		for(int i = 0; i < nbSegment + 1; i++)
		{
			double t = i * step;

			double weightFactor = (1-t)*(1-t)*(1-t) + 3*(1-t)*(1-t)*t*weight +3*(1-t)*t*t*weight + t*t*t;
			
			double x = (1-t)*(1-t)*(1-t)*p0.x() + 3*(1-t)*(1-t)*t*p1.x()*weight +3*(1-t)*t*t*p2.x()*weight + t*t*t*p3.x();
			double y = (1-t)*(1-t)*(1-t)*p0.y() + 3*(1-t)*(1-t)*t*p1.y()*weight +3*(1-t)*t*t*p2.y()*weight + t*t*t*p3.y();
			Point3D currentPoint = new Point3D(x / weightFactor, y / weightFactor);
		
			this.add(currentPoint);
		}
	}
	
	public BezierCurve(Point3D p0, Point3D p1, Point3D p2, Point3D p3, float weight)
	{
		this(p0,p1,p2,p3, 1.0f, 10);
	}
	
	public BezierCurve(Point3D p0, Point3D p1, Point3D p2, Point3D p3)
	{
		this(p0, p1, p2, p3, 1.0f);
	}
}
